﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMS.DD35.Common.Data;
using AMS.Core;
using AMS.DD35.Common;

namespace AMS.DD35.DbAccess.SqlClient
{
    public class SqlEDMCharacterConnector : ICharacterConnector
    {
        public CharacterData GetCharacterData(long characterID)
        {
            using (DD35DataEntities dbContext = new DD35DataEntities())
            {
                Character edmCharacter = dbContext.Characters.FirstOrDefault(c => c.Character_ID == characterID);
                return Convert(edmCharacter);
            }
        }

        public void SaveCharacter(CharacterData characterData)
        {
            if (characterData.CharacterID <= 0)
            {
                SaveNewCharacter(characterData);
                return;
            }

            using (DD35DataEntities dbContext = new DD35DataEntities())
            {
                Character edmCharacter = dbContext.Characters.FirstOrDefault(c => c.Character_ID == characterData.CharacterID);
                UpdateEdmCharacter(characterData, edmCharacter);
                dbContext.SaveChanges();
            }
        }

        private void SaveNewCharacter(CharacterData characterData)
        {
            Character character = Character.CreateCharacter(-1,
                characterData.UserName,
                characterData.CharacterName,
                characterData.BaseStrength,
                characterData.BaseDexterity,
                characterData.BaseConstitution,
                characterData.BaseIntelligence,
                characterData.BaseWisdom,
                characterData.BaseCharisma,
                characterData.RaceID,
                characterData.Experience,
                characterData.Sex,
                characterData.Height,
                characterData.HeightUnits,
                characterData.Weight,
                characterData.WeightUnits,
                characterData.HairColor,
                characterData.EyeColor,
                characterData.MoralAlignment.ToString(),
                characterData.EthicalAlignment.ToString(),
                characterData.DeityID);
            using (DD35DataEntities dbContext = new DD35DataEntities())
            {
                dbContext.AddToCharacters(character);
                dbContext.SaveChanges();
            }

            // TODO: save currencies
            // TODO: save feats
            // TODO: save skills
            // TODO: save classes
        }

        private void UpdateEdmCharacter(CharacterData characterData, Character edmCharacter)
        {
            //edmCharacter.Username = characterData.UserName;
            edmCharacter.Character_Name = characterData.CharacterName;
            edmCharacter.Base_DEX = characterData.BaseStrength;
            edmCharacter.Base_DEX = characterData.BaseDexterity;
            edmCharacter.Base_CON = characterData.BaseConstitution;
            edmCharacter.Base_INT = characterData.BaseIntelligence;
            edmCharacter.Base_WIS = characterData.BaseWisdom;
            edmCharacter.Base_CHA = characterData.BaseCharisma;
            edmCharacter.Race_ID = characterData.RaceID;
            edmCharacter.Experience = characterData.Experience;
            edmCharacter.Sex = characterData.Sex;
            edmCharacter.Height = characterData.Height;
            edmCharacter.Height_Units = characterData.HeightUnits;
            edmCharacter.Weight = characterData.Weight;
            edmCharacter.Weight_Units = characterData.WeightUnits;
            edmCharacter.Hair_Color = characterData.HairColor;
            edmCharacter.Eye_Color = characterData.EyeColor;
            edmCharacter.Moral_Alignment = characterData.MoralAlignment.ToString();
            edmCharacter.Ethical_Alignment = characterData.EthicalAlignment.ToString();
            edmCharacter.Deity_ID = characterData.DeityID;

            // coinage

            // feats

            // skills

            // classes
        }

        public IEnumerable<CharacterData> GetCharacters(string userName)
        {
            List<CharacterData> results = new List<CharacterData>();

            using (DD35DataEntities dbContext = new DD35DataEntities())
            {
                //IEnumerable<Character> results = dbContext.Characters.SelectMany(c => c.Username.ToLower() == userId.ToLower());
                foreach (Character ch in dbContext.Characters)
                {
                    if (ch.Username.ToLower() == userName.ToLower())
                        results.Add(Convert(ch));
                }
            }

            return results;
        }

        private CharacterData Convert(Character edmCharacter)
        {
            if (edmCharacter == null)
                return new CharacterData();

            CharacterData characterData = new CharacterData();
            characterData.CharacterID = edmCharacter.Character_ID;
            characterData.UserName = edmCharacter.Username;
            characterData.CharacterName = edmCharacter.Character_Name;
            characterData.BaseStrength = edmCharacter.Base_DEX;
            characterData.BaseDexterity = edmCharacter.Base_DEX;
            characterData.BaseConstitution = edmCharacter.Base_CON;
            characterData.BaseIntelligence = edmCharacter.Base_INT;
            characterData.BaseWisdom = edmCharacter.Base_WIS;
            characterData.BaseCharisma = edmCharacter.Base_CHA;
            characterData.RaceID = edmCharacter.Race_ID;
            characterData.Experience = edmCharacter.Experience;
            characterData.Sex = edmCharacter.Sex;
            characterData.Height = edmCharacter.Height;
            characterData.HeightUnits = edmCharacter.Height_Units;
            characterData.Weight = edmCharacter.Weight;
            characterData.WeightUnits = edmCharacter.Weight_Units;
            characterData.HairColor = edmCharacter.Hair_Color;
            characterData.EyeColor = edmCharacter.Eye_Color;
            characterData.MoralAlignment = EnumHelper.ParseEnum<MoralAlignment>(edmCharacter.Moral_Alignment);
            characterData.EthicalAlignment = EnumHelper.ParseEnum<EthicalAlignment>(edmCharacter.Ethical_Alignment);
            characterData.DeityID = edmCharacter.Deity_ID;

            // coinage

            // feats

            // skills

            // classes

            return characterData;
        }
    }
}
